﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class ThirdPartyPaymentController : BaseController
    {
        //
        // GET: /ThirdPartyPayment/

        // 操作员
        protected Model.View_OperUser operUser;

        public ActionResult Index()
        {
            return View();
        }

        #region 获取第三方支付列表
        /// <summary>
        /// 获取第三方支付列表
        /// </summary>
        /// <returns></returns>
        public ActionResult getgrid(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Pay_T> page = new Page<Model.Order_Pay_T>();//分页类定义
                List<Model.Order_Pay_T> List = new List<Model.Order_Pay_T>();
                string result = SRS.GetOrder_Pay(YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.Order_Pay_T>.JsonList(jieguo);//解析返回的结果
                    jqgridparam.records = List.Count;//查询的数据总数
                    var data = new
                    {
                        total =List.Count.ToString(),// searcho["total"].ToString(),
                        rows = page.FindListPage(List, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        //#region 获取当前页数据
        /// <summary>
        /// 获取当前页数据
        /// </summary>
        /// <param name="list">总的list数据</param>
        /// <param name="jqgridparam">分页参数</param>
        /// <returns></returns>
        //public List<Model.Order_Pay_T> FindListPage(List<Model.Order_Pay_T> list, ref JqGridParam jqgridparam)
        //{
        //    try
        //    {
        //        List<Model.Order_Pay_T> RetList = new List<Model.Order_Pay_T>();
        //        string orderField = jqgridparam.sort;//排序列
        //        string orderType = jqgridparam.order;//排序方式
        //        int pageIndex = jqgridparam.page;//当前页
        //        int pageSize = jqgridparam.rows;//每页行数
        //        int totalRow = jqgridparam.records;//总记录数
        //        int lastIndex = pageIndex * pageSize;//当前页最后一行
        //        if (orderType == "asc")
        //        {
        //            list = (from p in list orderby orderField ascending select p).ToList();
        //        }
        //        else if ((orderType == "desc"))
        //        {
        //            list = (from p in list orderby orderField descending select p).ToList();
        //        }

        //        //如果总行数小于当前页最后一行时 当前页最后一行等于总行数
        //        if (totalRow < lastIndex)
        //        {
        //            lastIndex = totalRow;
        //        }
        //        int fistIndex = (pageIndex - 1) * pageSize;
        //        int j = 0;
        //        for (int i = fistIndex; i < lastIndex; i++)
        //        {
        //            j++;
        //            RetList.Add(list[i]);
        //        }
        //        return RetList;
        //    }
        //    catch (Exception)
        //    {
        //        return null;
        //    }
        //}
        //#endregion

       /// <summary>
        /// 保存第三方支付变更
       /// </summary>
       /// <param name="Tag">更新类型（1：增，2：改，3：删除）</param>
       /// <param name="model">更新数据</param>
       /// <returns></returns>
        public ActionResult SetOrderPay(int Tag, Model.Order_Pay_T model)
        {
           
            // 获取操作员
            operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;
            if (operUser != null)
            {
                try
                {
                    string result = SRS.SetOrder_Pay_Key(operUser.OperID, ObjectToJson(model), Tag, "", 1);//从接口获得所有流水
                    JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                    string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                    if (bs == "1")//返回结果正常
                    {
                        return Json(bs);
                    }
                    else
                    {
                        return Json(searcho["Msg"].ToString());
                    }
                }
                catch (Exception e)
                {
                    string error = e.Message;
                    return Json(error);
                }
            }
            else
            {
                //return Json("获取操作员出错！");
                return Json("-1");     
                               
            }

        }
    }
}
